package com.future.lock.mall.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.future.lock.R;
import com.future.lock.mall.entity.beans.InstallAreaDescBean;
import com.yzh.modaldialog.dialog.impl.PopupDialog;

import java.util.List;

/**
 * Created by yzh on 2018/1/2.
 */

public class InstallFeeDescDialog extends PopupDialog {
    final InstallAreaDescBean installAreaDescBean;

    public InstallFeeDescDialog(@NonNull Context context, InstallAreaDescBean installAreaDescBean) {
        super(context, R.layout.dialog_install_area_desc);
        this.installAreaDescBean = installAreaDescBean;
    }

    @Override
    protected void onDialogCreate(Dialog dialog, Bundle savedInstanceState) {
        super.onDialogCreate(dialog, savedInstanceState);
        ViewPager viewPager = dialog.findViewById(R.id.viewPager);
        viewPager.setAdapter(new InstallFeeViewPagerAdapter());
        TabLayout tabLayout = dialog.findViewById(R.id.tab);
        tabLayout.setupWithViewPager(viewPager);
        dialog.findViewById(R.id.tab);
        dialog.findViewById(R.id.cancel)
                .setOnClickListener(view -> endModal(AlertDialog.BUTTON_NEGATIVE));
    }

    class InstallFeeViewPagerAdapter extends PagerAdapter {
        final String[] TITLE = new String[]{"自行安装区域", "商家安装区域"};

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext)
                    .inflate(R.layout.layout_install_area_list, null);
            TextView tips = view.findViewById(R.id.tips);
            ListView listView = view.findViewById(R.id.listView);
            TextView viewEmpty = view.findViewById(R.id.viewEmpty);
            listView.setEmptyView(viewEmpty);
            if (position == 0) {
                listView.setAdapter(new InstallFeeListAdapter(mContext, installAreaDescBean.getFeiqiangzhi()));
            }
            else {
                tips.setVisibility(View.GONE);
                listView.setAdapter(new InstallFeeListAdapter(mContext, installAreaDescBean.getQiangzhi()));
            }
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            container.addView(view);
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            try {
                container.removeView((View) object);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getCount() {
            return TITLE.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position];
        }
    }

    static class InstallFeeListAdapter extends BaseAdapter {
        final Context context;
        final List<String> area;

        InstallFeeListAdapter(Context context, List<String> area) {
            this.context = context;
            this.area = area;
        }

        @Override
        public int getCount() {
            return area == null ? 0 : area.size();
        }

        @Override
        public Object getItem(int i) {
            return area.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1;
            String data = (String) getItem(i);
            final int index = TextUtils.isEmpty(data) ? -1 : data.indexOf(":");
            String city = index == -1 ? "" : data.substring(0, index);
            String disctrict = index == -1 || index + 1 >= data.length() ? "" : data.substring(index + 1);
            if (view != null && view.getTag() instanceof Holder) {
                view1 = view;
            }
            else {
                view1 = LayoutInflater.from(context)
                        .inflate(R.layout.view_install_fee_area_list_item, null);
                Holder holder = new Holder(view1.findViewById(R.id.city), view1.findViewById(R.id.district));
                view1.setTag(holder);
            }

            ((Holder) view1.getTag()).city.setText(city);
            ((Holder) view1.getTag()).district.setText(disctrict);
            return view1;
        }

        class Holder {
            public Holder(TextView city, TextView district) {
                this.city = city;
                this.district = district;
            }

            TextView city;
            TextView district;
        }
    }
}
